package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.xinchuang.comment.R;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.entity.targetSystem.system.TargetSubType;
import com.xinchuang.entity.targetSystem.system.TargetType;
import com.xinchuang.entity.targetSystem.target.SystemIndex;
import com.xinchuang.mapper.targetSystem.TargetSubTypeMapper;
import com.xinchuang.mapper.targetSystem.TargetTypeMapper;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.targetSystem.TargetSubTypeService;
import com.xinchuang.service.targetSystem.target.SystemIndexService;
import com.xinchuang.utils.ObjectUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.tools.ant.taskdefs.Tar;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2023/11/7 18:36
 */
@Service
@RequiredArgsConstructor
public class TargetSubTypeImpl extends ServiceImpl<TargetSubTypeMapper, TargetSubType> implements TargetSubTypeService {

    private final TargetTypeMapper targetTypeMapper;

    private final RedisUseService redisUseService;

    private final SystemIndexService systemIndexService;

    @Override
    public Result addSubType(TargetSubType subType) {
        int count = count(new LambdaQueryWrapper<TargetSubType>()
                .eq(TargetSubType::getParentId, subType.getParentId())
                .eq(TargetSubType::getTypeName, subType.getTypeName()));
        if (count > 0) {
            return ResultUtil.error(500, "名称已重复，请重新修改");
        }
        subType.setLevel("0");
        if (save(subType)) {
            redisUseService.updateSubTypeName(subType.getTypeId(), subType.getTypeName());
            return ResultUtil.success("新增成功", null);
        }
        return ResultUtil.error(500, "系统异常，请稍后重试");
    }

    @Override
    public Result updateSubType(TargetSubType subType) {
        TargetSubType one = getOne(new LambdaQueryWrapper<TargetSubType>()
                .eq(TargetSubType::getParentId, subType.getParentId())
                .eq(TargetSubType::getTypeName, subType.getTypeName())
                .last("LIMIT 1"));
        if (ObjectUtil.isNotEmpty(one) && !subType.getTypeId().equals(one.getTypeId())) {
            return ResultUtil.error(500, "名称已重复，请重新修改");
        }
        //修改指标主体名称
        systemIndexService.lambdaUpdate()
                .eq(SystemIndex::getRedId, subType.getTypeId())
                .eq(SystemIndex::getParentId, "0")
                .set(SystemIndex::getIndicatorName, subType.getTypeName())
                .update();
        if (updateById(subType)) {
            redisUseService.updateSubTypeName(subType.getTypeId(), subType.getTypeName());
            return ResultUtil.success("更新成功", null);
        }
        return ResultUtil.error(500, "系统异常，请稍后重试");
    }

    @Override
    public Result getType(String parentId) {
        TargetType targetType = targetTypeMapper.selectTypeOnly(parentId);
        if (ObjectUtil.isEmpty(targetType)) {
            return ResultUtil.success(new ArrayList<>());
        }
        List<TargetSubType> list = list(new LambdaQueryWrapper<TargetSubType>().eq(TargetSubType::getParentId, parentId));
        List<Map<String, Object>> resultList = new ArrayList<>();
        Map<String, Object> parent = new HashMap<>();
        parent.put("typeId", targetType.getTypeId());
        parent.put("typeName", targetType.getTypeName());
        parent.put("level", "0");
        parent.put("sign", 0);
        parent.put("url", null);
        JSONArray array = new JSONArray();
        if (CollectionUtil.isNotEmpty(list)) {
            JSONObject jsonObject;
            for (TargetSubType targetSubType : list) {
                jsonObject = new JSONObject();
                jsonObject.put("typeId", targetSubType.getTypeId());
                jsonObject.put("typeName", targetSubType.getTypeName());
                jsonObject.put("children", new JSONArray());
                jsonObject.put("level", "1");
                jsonObject.put("hiden", false);
                jsonObject.put("url", null);
                array.add(jsonObject);
            }
        }
        parent.put("children", array);
        resultList.add(parent);
        return ResultUtil.success(resultList);
    }

    @Override
    public Result deleteSubType(String id) {
        if (removeById(id)) {
            redisUseService.deleteSubTypeName(id);
            return ResultUtil.success("删除成功", null);
        }
        return ResultUtil.error(500, "系统异常，请稍后重试");
    }
}
